package code;

/*
 * 
To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;

/**
 *
 * @author jyotesh
 */
public class NodeList {
    
    class Node {
        int nodeIndex;
        int degree;
        
        Node() {
        }
        
        void updateData(int nodeIndex, int degree) {
            this.nodeIndex = nodeIndex;
            this.degree = degree;
        }
        
        int getNodeIndex() {
            return this.nodeIndex;
        }
        
        int getDegree() {
            return this.degree;
        }
    }
    
    class NodeComparator implements Comparator {
        
        @Override
        public int compare(Object o1, Object o2) {
            int degree1 = ((Node)o1).getDegree();
            int degree2 = ((Node)o2).getDegree();
            if (degree1 > degree2)
                return 1;
            else if (degree1 < degree2)
                return -1;
            else
                return 0;
        }
    }
    
    private int n;
    private Node[] nodeList = null;
    
    NodeList(boolean[][] graph) {
        int degreeCount;
        n = graph.length;
        nodeList = new Node[n];
        for (int i = 0; i < n; i++) {
            degreeCount = 0;
            for (int j = 0; j < n; j++) {
                if (graph[i][j])
                    degreeCount++;
            }
            nodeList[i] = new Node();
            nodeList[i].updateData(i, degreeCount);
        }
        Arrays.sort(nodeList, new NodeComparator());
    }
    
    public void displayData() {
        System.out.println("Node-Degree");
        for (int i = 0; i < n; i++)
            System.out.println(nodeList[i].getNodeIndex() + " " + nodeList[i].getDegree());
    }
    
    int[] getIndexList() {
        int[] indexList = new int[n];
        for (int i = 0; i < n; i++)
            indexList[i] = nodeList[i].getNodeIndex();
        return indexList;
    }
}
